package com.weight.system.quick.web;

import com.weight.system.quick.PO.QuickInfo;
import com.weight.system.quick.VO.QuickInfoVO;
import com.weight.system.quick.service.QuickInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 快捷管理表
 */
@RestController
@Slf4j
@RequestMapping("/quick")
public class QuickInfoController {

	@Autowired
	private QuickInfoService quickInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询快捷管理表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForQuickInfoList",method=RequestMethod.GET)
	public ModelAndView queryForQuickInfoList(){
		
		Map<Object, Object> map =new HashMap<>();
//		map.put("menuId", menuId);
//		map.put("operId", operId);
		map.put("quickStatue", "1");
		 

		//2、查询快捷管理表列表
		List<QuickInfo> quickInfoList = quickInfoService.findQuickInfoByT(map);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("quick/queryForQuickInfoList");
		modelAndView.addObject("quickInfoList", quickInfoList);
		return modelAndView;
	}

	/**
	 * 2.查询快捷管理表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForQuickInfoListDG",method=RequestMethod.GET)
	public String queryForQuickInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String menuId = request.getParameter("menuId");
			String operId = request.getParameter("operId");
			String quickStatue = request.getParameter("quickStatue");

			Map<Object, Object> map =new HashMap<>();
			map.put("menuId", menuId);
			map.put("operId", operId);
			map.put("quickStatue", quickStatue);

			
			//2、查询快捷管理表列表
			List<QuickInfo> quickInfoList = quickInfoService.findQuickInfoByPage(page,rows,map);
			log.info("quickInfoList={}",quickInfoList);
			List<QuickInfoVO> quickInfoVOList = quickInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<QuickInfoVO> result = new PageResult<QuickInfoVO>(quickInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param quickInfoVO
	 * @param quickInfo
	 * @return
	 */
	public void changeVOToPO(QuickInfoVO quickInfoVO,QuickInfo quickInfo){
			BeanUtils.copyProperties(quickInfoVO,quickInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param quickInfoVO
	 * @param quickInfo
	 * @return
	 */
	public void changePoToVO(QuickInfoVO quickInfoVO,QuickInfo quickInfo){
			BeanUtils.copyProperties(quickInfo,quickInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param quickInfo
	 * @return
	 */
	public QuickInfoVO changePoToVO(QuickInfo quickInfo){
			QuickInfoVO quickInfoVO=new QuickInfoVO();
			BeanUtils.copyProperties(quickInfo,quickInfoVO);
			

			return quickInfoVO;
	}

	/**
	 * 3.查询快捷管理表--详情页
	 * @param quickInfoId
	 * @return
	 */
	@RequestMapping("/queryForQuickInfoDetail")
	public ModelAndView queryForQuickInfoDetail(@RequestParam(value="quickInfoId",required=false) String quickInfoId){
			//1.校验id
			CommonUtils.validateExistID(quickInfoId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("quickInfoId", quickInfoId);
			log.info("查询快捷管理表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			QuickInfo quickInfo = quickInfoService.findByQuickInfoId(map);
			
			QuickInfoVO quickInfoVO = changePoToVO(quickInfo);
			//3、返回前台
			log.info("查询快捷管理表查询成功：quickInfo={}",quickInfo);
			modelAndView.setViewName("quick/queryForQuickInfoDetail");
			modelAndView.addObject("quickInfoVO",quickInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 3.添加、修改快捷管理表操作
	 * @param quickInfoId
	 * @return
	 */
	@RequestMapping(value="/addQuickInfo")
	public ModelAndView addQuickInfo(@RequestParam(value="quickInfoId",required=false) String quickInfoId){
			//1、创建返回对象
			QuickInfoVO quickInfoVO = new QuickInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(quickInfoId)) {
				Map<String, String> map=new HashMap<>();
				map.put("quickInfoId", quickInfoId);
				log.info("修改操作，查询ID：quickInfoId={}",quickInfoId);
				QuickInfo quickInfo = quickInfoService.findByQuickInfoId(map);
				this.changePoToVO(quickInfoVO,quickInfo);
			}
			
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("quick/addQuickInfo");
			modelAndView.addObject("quickInfoVO",quickInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改快捷管理表操作
	 * @param quickInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveQuickInfo")
	public JsonResultUtil saveQuickInfo(@Valid QuickInfoVO quickInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,quickInfoVO);
			
			//2.获取id
			String quickInfoId=quickInfoVO.getQuickInfoId();
			QuickInfo quickInfo=new QuickInfo();
			BeanUtils.copyProperties(quickInfoVO,quickInfo);
			this.changeVOToPO(quickInfoVO,quickInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(quickInfoId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("quickInfoId", quickInfoId);
				log.info("修改操作，查询ID：quickInfoId={}",quickInfoId);
				//查询是否存在
				quickInfoService.findByQuickInfoId(map);
				try{
					quickInfo.setUpdateBy("000");
					quickInfoService.updateQuickInfo(quickInfo);
					log.info("更新操作成功：quickInfo={}",quickInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					quickInfo.setQuickInfoId(CommonUtils.getUUId());
					quickInfo.setCreateBy("000");
					quickInfo.setUpdateBy("000");
					quickInfo.setQuickStatue(1);
					quickInfoService.insertQuickInfo(quickInfo);
					log.info("添加操作成功：quickInfo={}",quickInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 6.删除快捷管理表操作
	 * @param quickInfoId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByQuickInfoId")
	public JsonResultUtil deleteByQuickInfoId(@RequestParam(value="quickInfoId",required=false) String quickInfoId){
			//1.校验id
			CommonUtils.validateExistID(quickInfoId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("quickInfoId", quickInfoId);
			quickInfoService.findByQuickInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除快捷管理表入参，map={}",map);
			
			try{
				quickInfoService.upDelByQuickInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除快捷管理表操作---批量删除
	 * @param quickInfoIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByQuickInfoIdBatch")
	public JsonResultUtil deleteByQuickInfoIdBatch(@RequestParam(value="quickInfoIdList",required=false) List quickInfoIdList){
			//1.校验id
			CommonUtils.validateExistIDList(quickInfoIdList);
			
			//2.校验数据库
			quickInfoService.findByQuickInfoIdList(quickInfoIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("quickInfoIdList", quickInfoIdList);
			map.put("updateBy","000");
			log.info("批量删除快捷管理表操作入参，map={}",map);
			
			try{
				quickInfoService.upDelByQuickInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}